const fs = require('fs');
const path = require("path");
const Base = require("./template/base");
class Template extends Base{
	render(filename){
		let tpl = this;
		let template_file = this.getTemplate(filename);
		this.res.statusCode = 200;
		this.res.setHeader("Content-Type", "text/html");
		fs.readFile(template_file, 'utf8', function(err,data){
			let _data = tpl.output(data);
			tpl.res.write(_data,"utf8");  
			tpl.res.end();
		});
	}
	getTemplate(filename){
		let template_file = "";
			template_file += this.config.dir.get("root");
			template_file += "/"+this.config.dir.get("app")+"/"+this.config.dir.get("appname");
			template_file += "/"+this.config.dir.get("view")+"/"+this.config.router.route.module+"/"+this.config.router.route.controll+"/";
			template_file += (filename?filename:this.config.router.route.action)+".html";
		return path.normalize(template_file);
	}
}

module.exports = Template;